package com.xhsj.user.pms.patent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.pms.patent.domain.PatentFile;
import com.xhsj.user.pms.patent.mapper.PatentFileMapper;
import com.xhsj.user.pms.patent.service.PatentFileService;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: 罗季嘉
 * @Date: 2020/1/17 16:23
 */
@Transactional
@Service
public class PatentFileServiceImpl implements PatentFileService {

    @Autowired
    private PatentFileMapper patentFileMapper;

    /**
     * 根据专利ID查询申请文件
     * @return
     */
    @Override
    public IPage<PatentFile> selectApplicationFileByDisclosureId(Map<String,Object> param) {
        int pageNum=1;
        int pageSize =20;
        if(StringUtils.isNotNull( param.get("pageNum")))
            pageNum = Integer.parseInt(String.valueOf(param.get("pageNum")));
        if(StringUtils.isNotNull(  param.get("pageSize")))
            pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
        String disclosureId = String.valueOf(param.get("disclosureId"));
        IPage<PatentFile> page = new Page<>(pageNum, pageSize);
        return patentFileMapper.selectApplicationFileByDisclosureId(page,disclosureId);
    }

    //    根据名称搜索获取专利申请文件参数
    @Override
    public IPage<PatentFile> searchApplicationFileByFileName(String disclosureId, String fileName, List<Integer> fileTypeList, int pageNum, int pageSize) {
        Page<PatentFile> page = new Page<>(pageNum,pageSize);
        return patentFileMapper.searchApplicationFileByFileName(page,disclosureId,fileName,fileTypeList);
    }

//    通知相关文件列表
    @Override
    public IPage<Map<String, Object>> getNoticeFileByDisclosureId(Map<String,Object> param) {
        int pageNum=1;
        int pageSize =20;
        if(StringUtils.isNotNull( param.get("pageNum")))
            pageNum = Integer.parseInt(String.valueOf(param.get("pageNum")));
        if(StringUtils.isNotNull(  param.get("pageSize")))
            pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
        String disclosureId = String.valueOf(param.get("disclosureId"));
        Page<PatentFile> page = new Page<>(pageNum,pageSize);
        return patentFileMapper.getNoticeFileByDisclosureId(page,disclosureId);
    }
//    根据文件名或日期获得通知相关文件列表
    @Override
    public IPage<Map<String, Object>> searchNoticeFileByFileName(Map<String,Object> param) {
        String disclosureId = String.valueOf(param.get("disclosureId"));
        String fileName = String.valueOf(param.get("fileName"));
        String uploadTime = String.valueOf(param.get("uploadTime"));
        int pageNum=1;
        int pageSize =20;
        if(StringUtils.isNotNull( param.get("pageNum")))
            pageNum = Integer.parseInt(String.valueOf(param.get("pageNum")));
        if(StringUtils.isNotNull(  param.get("pageSize")))
            pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
        IPage<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        return patentFileMapper.searchNoticeFileByFileName(page,disclosureId,fileName,uploadTime);
    }

    @Override
    public Message updateReviewPatentFile(PatentFile patentFile) {
        //先删后增
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("disc_id",patentFile.getDiscId());
        queryWrapper.eq("file_type",patentFile.getFileType());
        PatentFile file=new PatentFile();
        file.setCheckType(0);
        patentFileMapper.update(file, queryWrapper);
        patentFile.setPatentFileId(IdGen.generateUUID());
        if(patentFileMapper.insert(patentFile) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
            return Message.error(1,"保存专利文件失败");
        }
          return  Message.success("成功");
    }
}
